<?php


namespace app\api\controller\xiluedu;


use app\common\controller\Api;
use app\common\model\xiluedu\SignLog;
use app\common\model\xiluedu\SignRule;
use think\Exception;

class Sign extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 本周签到情况
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function week_sign(){
        //本周一到周日
        $monday = date('Y-m-d',(time()-((date('w',time())==0?7:date('w',time()))-1)*24*3600));
        $sunday = date('Y-m-d',(time()+(7-(date('w',time())==0?7:date('w',time())))*24*3600));

        $list = SignLog::where('user_id',$this->auth->id)
            ->where('createtime','between',[strtotime($monday.' 00-00-00'),strtotime($sunday.' 23:59:59')])
            ->field('createtime')
            ->select();
        $date = [];
        foreach ($list as $row){
            $date[] = $row->createtime_text;
        }
        $start_time = strtotime($monday);
        $end_time = strtotime($sunday);
        $sign = [];
        $arr = ['周日','周一','周二','周三','周四','周五','周六'];
        for ($start_time;$start_time<=$end_time;$start_time+=86400){
            $w = date('w',$start_time);
            if(in_array(date('Y-m-d',$start_time),$date)){
                $sign_status = true;
            }else{
                $sign_status = false;
            }
            $sign[] = [
                'name'          =>  $arr[$w],
                'date'          =>  date('d',$start_time),
                'sign_status'   =>  $sign_status,
            ];
        }
        $score = $this->auth->getUser()['score'];

        $last_sign = SignLog::where('user_id',$this->auth->id)->order('id','desc')->find();
        if($last_sign['createtime'] >= strtotime(date('Y-m-d'))){
            $series_days = $last_sign['series_days'];
            #明日签到获得积分
            $sign_score = SignRule::getSignScore($series_days+1);
            $today_sign_status = true;
        }else if($last_sign['createtime'] >= strtotime(date('Y-m-d',strtotime('-1 day')))){
            $series_days = $last_sign['series_days'];
            #今日签到获得积分
            $sign_score = SignRule::getSignScore($series_days+1);
            $today_sign_status = false;
        }else{
            $series_days = 0;
            #签到中断，今日签到获得积分
            $sign_score = SignRule::getSignScore(1);
            $today_sign_status = false;
        }



        $this->success('查询成功',[
            'series_days'=> $series_days,
            'sign'=>$sign,
            'score'=>$score,
            'sign_score'=>$sign_score,
            'today_sign_status'=>$today_sign_status
        ]);
    }

    /**
     * 立即签到
     */
    public function sign(){
        try {
            $ret = SignLog::sign();
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('签到成功');
    }

}